//
//  YunOrderListController.m
//  BaseProject
//
//  Created by 王盼盼 on 2024/10/21.
//  Copyright © 2024 wangpanpan. All rights reserved.
//

#import "YunOrderListController.h"
#import "YunOrderListViewModel.h"
#import "YunOrderListCell.h"
#import "YunOrderListModel.h"
#import "WPPPickerDayTimeView.h"
#import "ClassesPickerView.h"
#import "YunOrderClassesModel.h"
#import "InputPayViewController.h"
@interface YunOrderListController ()<UITableViewDelegate, UITableViewDataSource, BaseTableViewCellDelegate>
@property(nonatomic, strong) YunOrderListViewModel *viewModel;
@property (nonatomic, strong)UITableView *tableview;
@property(nonatomic, weak) ClassesPickerView *pickerView;
@end

@implementation YunOrderListController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"消费订单";
}
- (void)makeUI {
    [super makeUI];
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitleColor:UIColorHex(0x333333) forState:UIControlStateNormal];
    [rightBtn setTitle:@"订单筛选" forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(sort:) forControlEvents:UIControlEventTouchUpInside];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    rightBtn.frame = CGRectMake(0,0,66,30);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItems =@[rightItem];
    
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}
- (void)handleEvent {
    [super handleEvent];
    WK(weakSelf)
    self.tableview.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        [weakSelf.viewModel refresh:YES];
    }];
    self.tableview.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        [weakSelf.viewModel refresh:NO];
    }];
    [self.viewModel setRefresh:^(BOOL hasNext) {
        [weakSelf.tableview reloadData];
        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];
        if (weakSelf.viewModel.dataArray.count == 0) {
            [[TTNoResultManager shareManager] showNoResultWithStateInView:weakSelf.tableview errorCode:kTTViewState_NoOrder btnTitle:@"" didPressButton:^{}];
        } else {
            [[TTNoResultManager shareManager] hideNoResultWithView:weakSelf.tableview];
        }
        if (hasNext) {
            [weakSelf.tableview.mj_footer resetNoMoreData];
        } else {
            [weakSelf.tableview.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}
- (void)bindViewModel {
    [super bindViewModel];
    [self.viewModel transfrom];
}
#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModel.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YunOrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.data = self.viewModel.dataArray[indexPath.row];
    cell.indexPath = indexPath;
    cell.delegate = self;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
- (void)cell:(BaseTableViewCell *)cell indexPath:(NSIndexPath *)indexPath extra:(id)extra {
    InputPayViewController *vc = [[InputPayViewController alloc]init];
    vc.orderModel = self.viewModel.dataArray[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - click
- (void)sort:sender {
    if (self.pickerView != nil) {
        return;
    }
    WK(weakSelf)
    ClassesPickerView *view = [[ClassesPickerView alloc]initWithFrame:CGRectZero];
    view.parentVC = self;
    view.title = @"";
    view.data = self.viewModel.classes;
    [view setSelected:^(YunOrderClassesModel * _Nonnull selectedData) {
        [weakSelf.viewModel resetClassId:selectedData.scId];
        if (selectedData.scId.length > 0) {
            weakSelf.title = selectedData.parentName;
        } else {
            weakSelf.title = @"消费订单";
        }
    }];
    self.pickerView = view;
}
#pragma mark - lazy
- (YunOrderListViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [[YunOrderListViewModel alloc]init];
    }
    return _viewModel;
}
- (UITableView *)tableview {
    if (!_tableview) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
        [self.view addSubview:tableView];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.tableFooterView = [UIView new];
        tableView.tableHeaderView = [UIView new];
        [tableView registerClass:[YunOrderListCell class] forCellReuseIdentifier:@"Cell"];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.backgroundColor = UIColorHex(0xf1f1f1);
        tableView.rowHeight = 50;
        if (@available(iOS 11.0, *)) {
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            tableView.estimatedRowHeight = 0;
            tableView.estimatedSectionFooterHeight = 0;
            tableView.estimatedSectionHeaderHeight = 0;
        }
        if (@available(ios 15.0,*)) {
            UITableView.appearance.sectionHeaderTopPadding = 0;
        }
        _tableview = tableView;
    }
    return _tableview;
}
@end
